package com.sky.task;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
@Slf4j

public class OrderTask {

    @Autowired
    OrderMapper orderMapper;
    /**
     * 处理超时订单
     */
//    @Scheduled(cron = "0/5 * * * * ?")
    public void handleTimeoutOrders(){
        log.info("处理超时订单,{}", LocalDateTime.now());
        LocalDateTime timeoutTime = LocalDateTime.now().minusMinutes(-15);
        List<Orders> timeoutOrders = orderMapper.getTimeoutOrders(Orders.PENDING_PAYMENT, timeoutTime);
        if(timeoutOrders.isEmpty()){
            log.info("没有超时订单");
            return;
        }
        for (Orders order : timeoutOrders) {
            order.setStatus(Orders.CANCELLED);
            order.setCancelReason("超时未支付");
            order.setCancelTime(LocalDateTime.now());
            orderMapper.update(order);
        }
    }
        /**
         * 处理超时配送订单
         */
//        @Scheduled(cron = "0/1 * * * * ?")
        public void handleDeliveryTimeoutOrders(){
            log.info("处理超时订单,{}", LocalDateTime.now());
            LocalDateTime timeoutTime = LocalDateTime.now().minusMinutes(-60);
            List<Orders> timeoutOrders = orderMapper.getTimeoutOrders(Orders.DELIVERY_IN_PROGRESS, timeoutTime);
            if(timeoutOrders.isEmpty()){
                log.info("没有超时订单");
                return;
            }
            for (Orders order : timeoutOrders) {
                order.setStatus(Orders.COMPLETED);
                order.setCancelReason("超时未配送");
                order.setCancelTime(LocalDateTime.now());
                orderMapper.update(order);
            }
        }
    }

